var config = require('../../config/config.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showModal: "",
    hideModal: "",
    action: "brains",
    hideShopPopup: true,
    pageUrl: config.water_bill_list,
    waterbill:[],
    arr: [],
    shopType: "tobuy",//购物类型，加入购物车或立即购买，默认为加入购物车
    ticketList: [],
    mid: 1,
  },

  toBuy: function (e) {
    //通过 e.currentTarget.dataset.**拿到当前的数据
    var id = e.currentTarget.dataset.id;
    var goods_name = e.currentTarget.dataset.goods_name;
    var goods_img = e.currentTarget.dataset.goods_img;
    var goods_price = e.currentTarget.dataset.goods_price;
  //将当前页面的值通过url地址传到下一个页面
    wx.navigateTo({
      url: 'buy-ticket/buy-ticket?goods_name=' + goods_name + '&goods_img=' +goods_img + '&goods_price=' + goods_price + '&id=' + id,  
    });
  },

  // 触发bindscrolltolower事件时加载更多数据
  onScrollLower: function (message) {
    if (this.data.pageUrl) {
      this.getTicketUrl();
    }
  },



  //  * 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: function () {
  },

  //    * 页面上拉触底事件的处理函数
  onReachBottom: function () {
  },
  //调用接口
  getTicketUrl: function (event) {
    var that = this;
    id = 0;
    var member_info = (wx.getStorageSync('member_info'));
    if (event) {
      var id = event.currentTarget.dataset.id;
    }
    var arr = that.data.arr;
    var ticketList = that.data.ticketList;
    if (!that.data.pageUrl) {
      return;
    }
    wx.request({
      url: that.data.pageUrl,
      method: "GET",
      data: {
        mid: member_info.mid,
        aid: member_info.aid,
        sid: member_info.sid,
      },
      success: function (res) {
        var index = ticketList.length;
        var goodsList = res.data.data.goods_list.data;
        for (var i = 0; i < goodsList.length; i++) {
          ticketList[index + i] = goodsList[i];
        }
        that.setData({
          ticketList: ticketList,
          pageUrl: res.data.data.goods_list.next_page_url
        });
      },
      fail: function (res) {
        console.log(res.data)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var member_info = (wx.getStorageSync('member_info'));
    if (!wx.getStorageSync('member_info')) {
      wx.switchTab({
        url: '../takeWater/takeWater',
      })
      return
    }
    this.getTicketUrl()
    var ticketList = this.data.ticketList;
    this.setData({
      ticketList: ticketList
    })
  },
  //购买水票。用户须知
  onshow() {
    const self = this;
    let { action } = self.options
    action = action || 'brains'
    self.data.action = action
    self.setData(self.data)
  },

  toggleTabs(e) {
    const self = this;
    const { action } = e.currentTarget.dataset
    switch (action) {
      case 'brains': self.showBrains(); break;
      case 'stamina': self.showStamina(); break;
      default: break;
    }
  },
  showBrains() {
    const self = this;
    self.options.action = "brains"
    self.onshow()
  },
  showStamina() {
    const self = this
    self.options.action = "stamina"
    self.onshow();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})